<?php

namespace App\Admin\Controllers;

use App\Models\AdminUserRequirement;
use App\Models\Module;
use App\Models\Project;
use App\Models\Requirement;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Modulebon\Modulebon;
use Illuminate\Support\MessageBag;
use Encore\Admin\Widgets\Table;
use Encore\Admin\Facades\Admin;

class RequirementController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '需求管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Requirement());

        $grid->expandFilter();
        $admin_user_id = Admin::user()->id;
        $grid->model()->orderBy('id', 'desc');
 
        if(!($admin_user_id ==1 || Admin::user()->can('xuqiumanage'))){
            $grid->disableCreateButton();
            $grid->actions(function ($actions) use($admin_user_id){
                
                    // 去掉删除
                    $actions->disableDelete();
                    

                
            });
        }
        $grid->column('id', __('Id'));
        $grid->column('name', __('需求简称'));
        $grid->column('type', __('类型'));
        $grid->column('module.name', __('模块'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Requirement::findOrFail($id));
        // $requirement = Requirement::where('id',$id)->first();
        $show->field('name', '需求简称');
        $show->field('status', '归档状态')->as(function($status){
            return  empty($status)?'未归档':'已归档';
        });
        $show->field('type', '类型');
        $show->field('module_id', '模块')->as(function($module_id){
            $module = Module::where('id',$module_id)->first();
            return $module->name;
        });
        $show->field('prototype_path', '原型图')->as(function ($prototype_path) {

            return empty($prototype_path)?'无':url('/').'/prototypes/'.$prototype_path;
        
        })->link();
        $show->field('content', '需求说明')->unescape();
        $show->field('created_at','创建时间');
        $show->field('updated_at','更新时间');
        $admin_user_id = Admin::user()->id;
        if(!($admin_user_id ==1 || Admin::user()->can('xuqiumanage'))){
            $show->panel()
                ->tools(function ($tools) {
                    $tools->disableEdit();
                    $tools->disableList();
                    $tools->disableDelete();
                });
        }
        $show->panel()
                ->tools(function ($tools) {
                    $tools->disableDelete();
                });
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Requirement());
        $admin_user_id = Admin::user()->id;
        if($admin_user_id !=1){
            $form->tools(function (Form\Tools $tools) {
        
                // 去掉`删除`按钮
                $tools->disableDelete();
            });
        }
        $parameter = request()->route()->parameters();
        if($form->isEditing()){
            $id= $parameter['requirement'];
        }else{
            $id ='';
        }
        $form->text('name','需求简称')->setWidth(7, 2)->required();
        $form->select('type', __('类型'))->options(['新增' => '新增', '修改' => '修改', '修复' => '修复'])->required();
        $form->simditor('content');
        $form->select('order_color_id', __('优先级'))->options('/api/requirement_order_colors')->required();
        $form->text('prototype_path','原型图');
        $form->radio('status','是否归档')->options(['归档' => '归档', 'null' => '不归档'])->default('不归档');
        if($form->isCreating()){
            $form->select('project_id', __('项目'))->options('/api/projects')->load('module_id','/api/modules');
            $form->select('module_id', __('模块'))->required();
            $form->ignore(['project_id']);
        }
        $form->listbox('xuqiutaolun', '需求讨论')->options('/api/admin_users_array');
        $form->listbox('houduankaifa', '后端开发')->options('/api/admin_users_array');
        $form->listbox('qianduankaifa', '前端开发')->options('/api/admin_users_array');
        $form->listbox('uisheji', 'UI设计')->options('/api/admin_users_array');
        $form->listbox('ceshi', '测试')->options('/api/admin_users_array');
        $form->listbox('bushushangxian', '部署上线')->options('/api/admin_users_array');

        $form->saved(function (Form $form) use($id) {
            // $aur = new AdminUserRequirement();
            if($form->status == '归档' && !empty($id)){
                
                (new AdminUserRequirement())->where('requirement_id',$id)->update(['status'=>'归档']);
            }

            if(!empty($form->xuqiutaolun)){
                foreach($form->xuqiutaolun as $admin_user_id){
                    if(!empty($admin_user_id)){
                        $aur = new AdminUserRequirement();
                        //先查查有没有重复
                        $admin_user_requirement = $aur->where([
                            ['type','需求讨论'],
                            ['admin_user_id',$admin_user_id],
                            ['requirement_id',$form->model()->id]
                        ])->first();
                        if(empty($admin_user_requirement)){
                            $aur = new AdminUserRequirement();
    
                            $aur->requirement_id = $form->model()->id;
                            $aur->admin_user_id = $admin_user_id;
                            $aur->type = '需求讨论';
                            $aur->save();
                        }
                    }
                    
                }
            }
            if(!empty($form->houduankaifa)){
                foreach($form->houduankaifa as $admin_user_id){
                    if(!empty($admin_user_id)){
                        $aur = new AdminUserRequirement();
                        //先查查有没有重复
                        $admin_user_requirement = $aur->where([
                            ['type','后端开发'],
                            ['admin_user_id',$admin_user_id],
                            ['requirement_id',$form->model()->id]
                        ])->first();
                        if(empty($admin_user_requirement)){
                            $aur = new AdminUserRequirement();

                            $aur->requirement_id = $form->model()->id;
                            $aur->admin_user_id = $admin_user_id;
                            $aur->type = '后端开发';
                            $aur->save();
                        }
                    }
                }
            }
            if(!empty($form->qianduankaifa)){
                foreach($form->qianduankaifa as $admin_user_id){
                    if(!empty($admin_user_id)){
                        $aur = new AdminUserRequirement();
                        //先查查有没有重复
                        $admin_user_requirement = $aur->where([
                            ['type','前端开发'],
                            ['admin_user_id',$admin_user_id],
                            ['requirement_id',$form->model()->id]
                        ])->first();
                        if(empty($admin_user_requirement)){
                            $aur = new AdminUserRequirement();

                            $aur->requirement_id = $form->model()->id;
                            $aur->admin_user_id = $admin_user_id;
                            $aur->type = '前端开发';
                            $aur->save();
                        }
                    }
                }
            }
            if(!empty($form->uisheji)){
                foreach($form->uisheji as $admin_user_id){
                    if(!empty($admin_user_id)){
                        $aur = new AdminUserRequirement();
                        //先查查有没有重复
                        $admin_user_requirement = $aur->where([
                            ['type','UI设计'],
                            ['admin_user_id',$admin_user_id],
                            ['requirement_id',$form->model()->id]
                        ])->first();
                        if(empty($admin_user_requirement)){
                            $aur = new AdminUserRequirement();

                            $aur->requirement_id = $form->model()->id;
                            $aur->admin_user_id = $admin_user_id;
                            $aur->type = 'UI设计';
                            $aur->save();
                        }
                    }
                }
            }
            if(!empty($form->ceshi)){
                foreach($form->ceshi as $admin_user_id){
                    if(!empty($admin_user_id)){
                        $aur = new AdminUserRequirement();
                        //先查查有没有重复
                        $admin_user_requirement = $aur->where([
                            ['type','测试'],
                            ['admin_user_id',$admin_user_id],
                            ['requirement_id',$form->model()->id]
                        ])->first();
                        if(empty($admin_user_requirement)){
                            $aur = new AdminUserRequirement();

                            $aur->requirement_id = $form->model()->id;
                            $aur->admin_user_id = $admin_user_id;
                            $aur->type = '测试';
                            $aur->save();
                        }
                    }
                }
            }
            if(!empty($form->bushushangxian)){
                foreach($form->bushushangxian as $admin_user_id){
                    if(!empty($admin_user_id)){
                        $aur = new AdminUserRequirement();
                        //先查查有没有重复
                        $admin_user_requirement = $aur->where([
                            ['type','部署上线'],
                            ['admin_user_id',$admin_user_id],
                            ['requirement_id',$form->model()->id]
                        ])->first();
                        if(empty($admin_user_requirement)){
                            $aur = new AdminUserRequirement();

                            $aur->requirement_id = $form->model()->id;
                            $aur->admin_user_id = $admin_user_id;
                            $aur->type = '部署上线';
                            $aur->save();
                        }
                    }
                }
            }
        });

        return $form;
    }
}
